package cn.xk.org.service.impl;

import cn.xk.basic.service.impl.BaseServiceImpl;
import cn.xk.basic.util.BaiduAiUtils;
import cn.xk.org.domain.Employee;
import cn.xk.org.domain.Shop;
import cn.xk.org.domain.ShopAuditLog;
import cn.xk.org.dto.ShopDto;
import cn.xk.org.mapper.EmployeeMapper;
import cn.xk.org.mapper.ShopAuditLogMapper;
import cn.xk.org.mapper.ShopMapper;
import cn.xk.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一.校验
        //1.空值校验
        if (StringUtils.isEmpty(shop.getName()) || StringUtils.isEmpty(shop.getTel())) {
            throw new RuntimeException("信息不能为空!!!");
        }
        //2.两次密码不一致校验
        if (!shop.getAdmin().getComfirmPassword().equals(shop.getAdmin().getPassword())) {
            throw new RuntimeException("两次密码不一致!!!");
        }
        //3.当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if (dbShop != null) {//入驻过
            throw new RuntimeException("当前店铺已经入驻过，请直接登录。忘记密码请联系管理员!!!");
        }
        //二.百度AI智能审核【店铺名，logo是否合法】
        if (!BaiduAiUtils.TextCensor(shop.getName())) {
            throw new RuntimeException("店铺名称不合法!!!");
        }
        if (!BaiduAiUtils.ImgCensor("http://123.207.27.208" + shop.getLogo())) {
            throw new RuntimeException("店铺Logo不合规!!!");
        }

        //三：添加到数据库
        //1.先添加员工信息
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin); //shop_id = null 会返回自增id3个属性
        //2.再添加店铺信息
        shop.setAdminId(admin.getId());
        shopMapper.save(shop);//会返回自增id
        //3.将店铺id更新到员工表中
        admin.setShop_id(shop.getId()); //id ,shopId
        employeeMapper.update(admin);
        //四：人工审核之后发送激活邮件
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.改状态：1待审核 - 2审核通过待激活【1.先查再改 2.直接修改】
        shopMapper.updateState(2, log.getShopId());
        //2.添加审核日志
        log.setState(1);//审核通过
        log.setAuditId(350L);
        shopAuditLogMapper.save(log);
        //3.发邮件给店长通知他去激活
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("3170919649@qq.com"); //平台的邮箱
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/" +
                shop.getLogo() + "' ><a href='http://127.0.0.1:8080/shop/active/" + log.getShopId() + "'>点击该链接激活</a>", true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        helper.setTo(employee.getEmail()); //改邮件是店长的邮箱 - 真实有效
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //3.发邮件给店长通知他重新修改信息入驻
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3170919649@qq.com"); //平台的邮箱
        message.setSubject("店铺审核失败通知");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        message.setText("<h1>店铺审核失败，请重新入驻</h1>");
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        message.setTo(employee.getEmail()); //改邮件是店长的邮箱 - 真实有效
        javaMailSender.send(message);
    }

    @Override
    public String activeId(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.update(shop);
        return "激活成功";
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
